// pages/clickMe/plan/layout.js
import { API_HOST_1 } from '../../../api/config'
import api from '../../../api/index'
import { userInfoStore } from '../../../store/userInfo'
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 1,
    selectPlan:1,
    housePlans:[],
    isBack: false,
    proposal:[],
    showid:'',
    gotoMarsId:'',
    delImageId:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      step:options.step
    },()=>{
      if (options.step==0){
        this.getHouseImage(1)
      }
      if (options.step == 1) {
        let param = {
          userId: userInfoStore.userInfo.id,
          status: 2
        }
        let self = this
        api.getUserProposal(param).then(res => {
          //console.log(res)
          let { result } = res;
          self.setData({ selectPlan: 2, proposal: result })
        }, err => {
          self.getProposal(1)
        })


      }
    });
    
    
  },

  onShow: function (){
    if (this.data.isBack){
      this.setData({
        isBack: false,
        step: 0
      }, () => {
        this.getHouseImage(1)
      });
    }
  },

  getProposal:function(page) {
    let param = {
      userId: userInfoStore.userInfo.id,
      status: this.data.selectPlan
    }
    let self=this
    api.getUserProposal(param).then(res => {
      //console.log(res)
      let { result } = res;
      self.setData({ proposal: result })
    }, err => {
      wx.showToast({ title: err, icon: 'none'})
      self.setData({ proposal: [] })
    })
  },

  getHouseImage:function(p){
    let param = {
      page: p,
      size: 10,
      userId: userInfoStore.userInfo.id
    }
    let self=this
    api.getUserHouseImageList(param).then(res => {
      //console.log(res)
      let {result} = res;
      if (result.housePlans) 
        self.setData({ housePlans: result.housePlans})
      else
        self.setData({ housePlans: [] })
    })
  },

  selectItem:function(e){
    let s = e.currentTarget.dataset.step;
    let self = this
    this.setData({
      step: s,
      showid: '',
    }, ()=>{
      if (s==0){
        self.getHouseImage(1)
      }
      if (s == 1) {
        this.getProposal(1)
      }
    });
  },

  switchItem:function(e){
    let value;
    if (this.data.selectPlan == 1) value = 2;
    if (this.data.selectPlan == 2) value = 1;
    this.setData({
      selectPlan: value,
      showid: '',
    },()=>{
      this.getProposal(1)
    });
  },

  add:function(){
    wx.navigateTo({
      url: 'addlayout',
    })
  },

  uploadImage:function(param,imagePath){
    wx.showLoading()
    let self = this
    wx.uploadFile({
      url: API_HOST_1 + 'daikin/house-plan/modify',
      filePath: imagePath,
      name: 'image',
      formData: param,
      success: function (res) {
        if (res.statusCode == 200) {
          // console.log(res.data)
          self.getHouseImage(1)
        } else {
          wx.showModal({
            title: "提示",
            content: "失败,请重新上传",
            showCancel: false,
            confirmText: "我知道了"
          })
        }
      },
      complete: function () {
        wx.showLoading()
      }
    })
  },

  updateimage:function(e){
    let housePlanId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'addlayout?id=' + housePlanId,
    })

  },

  delimage:function(e){
    this.setData({ delImageId: e.currentTarget.dataset.id })
    let dialog = this.selectComponent("#dialog");
    dialog.show('确认删除吗?');
  },


  gotoMars:function(e){
    this.setData({ gotoMarsId: e.currentTarget.dataset.id})
    let dialog = this.selectComponent("#dialog");
    dialog.show('确认删除吗?');
  },

  dialogConfirm: function (e) {
    let self = this
    if (this.data.step == 1){
      let param = "proposalId=" + this.data.gotoMarsId
      api.gotoMarsProposal(param).then(res => {
        self.setData({ selectPlan: 1 }, () => {
          this.getProposal(1)
        })
      })
    }

    if (this.data.step == 0) {
      let param = {
        housePlanId: this.data.delImageId,
        userId: userInfoStore.userInfo.id
      }
      api.delUserHouseImage(param).then(res => {
        self.getHouseImage(1)
      })
    }
  },

  withMe:function(e){
    let param = "proposalId=" + e.currentTarget.dataset.id
    let self=this
    api.withMeProposal(param).then(res => {
      self.setData({ selectPlan:2}, ()=>{
        this.getProposal(1)
      })
    })
  },

  goOut:function(e) {
    let param = "proposalId=" + e.currentTarget.dataset.id
    let self = this
    api.goOut(param).then(res => {
      self.setData({ selectPlan: 2 }, () => {
        this.getProposal(1)
      })
    })
  },

  showDetail:function(e){
    let item = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    let param = { id: item.id }
    if (this.data.showid === item.id) {
        this.setData({showid: ''});
    } else {
      if (item.diagramId) {
         
      }
      if (!item.machineLists) {
        let self = this
        api.getProposalDetail(param).then(res => {
          let items = res.result.machinesList;
          let indoor = []
          let outdoor = []
          let air = []
          let port = []
          for (let i = 0; i < items.length; i++) {
            let item = items[i]
            if (item.maichineStyle=='内机') {
              if (0 == indoor.length) item.showtitle = '室内机'
              indoor.push(item)
            }
            if (item.maichineStyle == '外机') {
              if (0 == outdoor.length) item.showtitle = '室外机'
              outdoor.push(item)
            }
            if (item.maichineStyle == '新风') {
              if (0 == air.length) item.showtitle = '新风系统'
              air.push(item)
            }
            if (item.maichineStyle == '配件') {
              if (0 == port.length) item.showtitle = '配件'
              port.push(item)
            }
          }
          self.data.proposal[index].machineLists = []
          self.data.proposal[index].machineLists.push(...indoor)
          self.data.proposal[index].machineLists.push(...outdoor)
          self.data.proposal[index].machineLists.push(...air)
          self.data.proposal[index].machineLists.push(...port)
          self.data.proposal[index].proposalImageList = res.result.proposalImageList
          console.log(self.data.proposal)
          self.setData({ showid: param.id, proposal: self.data.proposal }, () => {})
        })
      } else {
        this.setData({ showid: item.id });
      }
    }
  },

  showLayoutImage: function(e){

    let images = e.currentTarget.dataset.images
    let urls = []
    for (let i = 0; i < images.length; i++) {
      if (images[i].uri != 'null' && images[i].uri.length != 0)
        urls.push(images[i].uri)
    }
    if (urls.length > 0) {
      wx.previewImage({
        urls: urls,
      })
    }
  },

  showDetailImage: function(e){


    let images = e.currentTarget.dataset.images
    let urls = []
    for (let i = 0; i < images.length; i++) {
      if (images[i].picPath != 'null' && images[i].picPath.length != 0)
        urls.push(images[i].picPath)
    }
    if(urls.length>0) {
      wx.previewImage({
        urls: urls,
      })
    }

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})